package cn.sc.summer.adapter.controller.feign;

import cn.sc.summer.constant.regex.RegexConstant;
import cn.sc.summer.constant.model.Result;
import cn.sc.summer.domain.user.SysUserInfoService;
import cn.sc.summer.feign.client.SysFeignClient;
import cn.sc.summer.feign.vo.UserInfoFeignVO;
import cn.sc.summer.log.annotation.Log;
import cn.sc.summer.token.po.AuthorityX;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.Set;

/**
 * 类名：sys server feign调用用户管理类
 *
 * @author a-xin
 * @date 2023/8/9 14:01
 */
@RestController
@RequestMapping("/userFeign")
public class SysUserInfoFeignController implements SysFeignClient {

    @Resource
    private SysUserInfoService sysUserInfoService;

    /**
     * 根据用户手机号查询用户信息
     *
     * @param phone 手机号
     * @return 用户信息
     */
    @Override
    @Log(value = "根据电话号码查询用户信息")
    @GetMapping("/getUserInfoByPhone")
    public Result<UserInfoFeignVO> getUserInfoByPhone(@RequestParam(value = "phone")
                                                      @NotBlank(message = "手机号不能为空！")
                                                      @Pattern(regexp = RegexConstant.TEL_REGEX, message = "手机号格式不正确！")
                                                      String phone) {
        return Result.success(sysUserInfoService.getUserInfoByPhone(phone));
    }

    /**
     * 缓存用户权限信息
     *
     * @param userId    用户ID
     * @param loginType 登录类型
     */
    @Override
    @Log(value = "缓存用户权限信息")
    @GetMapping("/getUserAuth")
    public Result<Set<AuthorityX>> getUserAuth(@RequestParam(value = "userId")
                                        @NotBlank(message = "用户ID不能为空！") String userId,
                                               @RequestParam(value = "loginType")
                                        @NotBlank(message = "登录类型不能为空！") String loginType) {
        return Result.success(sysUserInfoService.getUserAuth(userId, loginType));
    }

}
